#pragma once
#include<iostream>

using namespace std;

namespace wjb
{
	template<typename T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _val;

		list_node(const T& val = T())
			:_next(nullptr)
			,_prev(nullptr)
			,_val(val)
		{}

	};

	template<typename T>
	struct list_iterator
	{
		typedef list_node<T> node;
		node* _node;
		list_iterator(node* n)
		:_node(n)
		{}
		list_iterator& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		list_iterator operator++(int)
		{
			_node = _node->_next;
			return _node->_prev;
		}

		T operator*()
		{
			return _node->_val;
		}

		bool operator!=(list_iterator& it)
		{
			return _node != it._node;
		}

		list_iterator operator+(size_t n)
		{
			node* ret = _node;
			for (size_t i = 0; i < n; ++i)
			{
				ret = ret->_next;
			}
			return ret;
		}
		
	};

	template<typename T>
	class list
	{
		typedef list_node<T> node;
	public:
		typedef list_iterator<T> iterator;
		list()
		{
			_head = new node;
			_head->_prev = _head;
			_head->_next = _head;
		}
		void push_back(const T& val = T())
		{
			node* tail = _head->_prev;
			node* newnode = new node(val);
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;
		}

		iterator begin()
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}

		iterator earse(iterator pos)
		{
			node* ret = pos._node->_next;
			pos._node->_prev->_next = pos._node->_next;
			pos._node->_next->_prev = pos._node->_prev;
			delete pos._node;
			return ret;
		}

		iterator earse(iterator first, iterator last)
		{
			node* ret = pos._node->_next;
			pos._node->_prev->_next = pos._node->_next;
			pos._node->_next->_prev = pos._node->_prev;
			delete pos._node;
		}




	private:
		node* _head;
	};
}